package com.jgsu.farm.controller;

import com.jgsu.farm.entity.Model;
import com.jgsu.farm.exception.*;
import com.jgsu.farm.service.ModelService;
import com.jgsu.farm.utils.CommonResult;
import com.jgsu.farm.utils.FileUploadUtil;
import com.jgsu.farm.utils.ResultCode;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@Slf4j
@Api(value = "farm/sysuser/model", tags = "诊断模型管理模块")  //描述信息用的，标注在类上
@RestController
@RequestMapping("farm/sysuser/model")
public class ModelController {
    @Autowired
    ModelService modelService;
    @Autowired
    FileUploadUtil fileUploadUtil;

    /**
     * 诊断模型添加
     */
    @ApiOperation(value = "诊断模型添加功能")
    @PostMapping(value = "/add", produces = "application/json")
    public CommonResult modelAdd(String modelname, Integer author, MultipartFile modelfile, String description) throws IOException {
        String uploadFile = fileUploadUtil.upload(modelfile);
        Model model = new Model(modelname, author, uploadFile, description);
        try {
            modelService.add(model);
            log.info("诊断模型添加成功，诊断模型名字为：{}", model.getModelname());
            return CommonResult.success(model);
        } catch (DataAddException e) {
            log.error("诊断模型添加失败，诊断模型名字为：{}", model.getModelname());
            throw new DataAddException(e.getCode(), e.getMessage());
        }
    }

    /**
     * 诊断模型删除
     */
    @ApiOperation(value = "诊断模型删除功能")
    @DeleteMapping(value = "/delete", produces = "application/json")
    public CommonResult modelDelete(@RequestParam("modelid") Integer modelid) {
        try {
            modelService.delete(modelid);
            log.info("删除id为{}的资讯信息", modelid);
            return CommonResult.success(ResultCode.SUCCESS);
        } catch (DataNotExistException e) {
            log.error("不存在id为：{}的资讯信息，删除失败", modelid);
            throw new DataNotExistException(e.getCode(), e.getMessage());
        }
    }

    /**
     * 诊断模型查询功能
     */
    @ApiOperation(value = "诊断模型查询功能")
    @GetMapping(produces = "application/json")
    public CommonResult<PageInfo<Model>> modelList(@RequestParam(value = "page", required = false, defaultValue = "1") int page,
                                                   @RequestParam(value = "pagesize", required = false, defaultValue = "10") int pagesize) {
        try {
            PageInfo<Model> rules = modelService.selectAll(page, pagesize);
            log.info("查询所有的诊断模型信息");
            return CommonResult.success(rules);
        } catch (DataNotExistException e) {
            log.error("诊断模型表中没有数据");
            throw new DataNotExistException(e.getCode(), e.getMessage());
        }
    }

    /**
     * 诊断模型修改
     */
    @ApiOperation(value = "诊断模型修改功能")
    @PutMapping(value = "/update", produces = "application/json")
    public CommonResult modelUpdate(Integer modelid, String modelname, Integer author,
                                    MultipartFile modelfile, String description) throws IOException {
        String uploadFile = fileUploadUtil.upload(modelfile);
        Model model = new Model(modelid, modelname, author, uploadFile, description);
        try {
            modelService.update(model);
            log.info("诊断模型信息更新，更新id为：{}", model.getModelid());
            return CommonResult.success(model);
        } catch (DataNotExistException e) {
            log.error("更新id = {}的诊断模型信息失败", model.getModelid());
            throw new DataNotExistException(e.getCode(), e.getMessage());
        }
    }
}
